package com.uetoken.cn.utils;

import com.google.gson.Gson;
import com.uetoken.cn.bean.NewAddAddressBean;
import com.uetoken.cn.bean.area.CityBean;
import com.uetoken.cn.bean.area.DistrictBean;
import com.uetoken.cn.bean.area.ProvinceBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 谢杨
 * @time 2018/9/15 17:25
 * @class 描述:省市区筛选帮助类
 */
public class AreaUtils {
    /**
     * 解析三级数据
     *
     * @param response
     * @return
     */
    public static List<ProvinceBean> parseAreaData(String response) {
        List<ProvinceBean> mProvinceBeanList = new ArrayList<>();

        NewAddAddressBean areasBean = new Gson().fromJson(response, NewAddAddressBean.class);
        List<NewAddAddressBean.DataBean> dataBeans = areasBean.getData();

        for (NewAddAddressBean.DataBean dataBean1 : dataBeans) {
            //添加省
            if (dataBean1.getPareaid() == 1) {
                ProvinceBean provinceBean = new ProvinceBean();
                provinceBean.setPareaid(dataBean1.getPareaid());
                provinceBean.setAreaname(dataBean1.getAreaname());
                provinceBean.setAreaid(dataBean1.getAreaid());

                for (NewAddAddressBean.DataBean dataBean2 : dataBeans) {
                    //添加市
                    if (dataBean1.getAreaid() == dataBean2.getPareaid()) {
                        CityBean cityBean = new CityBean();
                        cityBean.setPareaid(dataBean2.getPareaid());
                        cityBean.setAreaname(dataBean2.getAreaname());
                        cityBean.setAreaid(dataBean2.getAreaid());
                        //添加区
                        for (NewAddAddressBean.DataBean dataBean3 : dataBeans) {
                            if (dataBean2.getAreaid() == dataBean3.getPareaid()) {
                                DistrictBean districtBean = new DistrictBean();
                                districtBean.setPareaid(dataBean3.getPareaid());
                                districtBean.setAreaname(dataBean3.getAreaname());
                                districtBean.setAreaid(dataBean3.getAreaid());
                                cityBean.getDistrictBeans().add(districtBean);
                            }
                        }
                        provinceBean.getCityBeans().add(cityBean);
                    }

                }
                mProvinceBeanList.add(provinceBean);
            }
        }

        for (ProvinceBean provinceBean : mProvinceBeanList) {
            List<CityBean> cityBeanList = provinceBean.getCityBeans();
            //二三级都没有   补上
            if (cityBeanList.size() == 0) {
                CityBean cityBean = new CityBean();
                cityBean.setPareaid(provinceBean.getPareaid());
                cityBean.setAreaname(provinceBean.getAreaname());
                cityBean.setAreaid(provinceBean.getAreaid());
                cityBeanList.add(cityBean);

                DistrictBean districtBean = new DistrictBean();
                districtBean.setPareaid(cityBean.getPareaid());
                districtBean.setAreaname(cityBean.getAreaname());
                districtBean.setAreaid(cityBean.getAreaid());
                cityBean.getDistrictBeans().add(districtBean);
            } else {
                //表示没有第三级 将第二级变成第三级 并补第二级
                //重新生成区
  /*              if (cityBeanList.get(0).getDistrictBeans().size()==0){
                    List<DistrictBean> districtBeanList = new ArrayList<>();
                    for (CityBean cityBean:cityBeanList){
                        DistrictBean districtBean = new DistrictBean();
                        districtBean.setPareaid(cityBean.getPareaid());
                        districtBean.setAreaname(cityBean.getAreaname());
                        districtBean.setAreaid(cityBean.getAreaid());
                        districtBeanList.add(districtBean);
                    }
                    cityBeanList.get(0).setDistrictBeans(districtBeanList);
                    //生成市 既是省
                    provinceBean.getCityBeans().clear();

                    CityBean cityBean = new CityBean();
                    cityBean.setPareaid(provinceBean.getPareaid());
                    cityBean.setAreaname(provinceBean.getAreaname());
                    cityBean.setAreaid(provinceBean.getAreaid());
                    cityBean.setDistrictBeans(districtBeanList);

                    provinceBean.getCityBeans().add(cityBean);


                }*/
                for (CityBean cityBean : cityBeanList) {
                    List<DistrictBean> districtBeanList = new ArrayList<>();
                    if (cityBean.getDistrictBeans().size() == 0) {
                        //第三级没有数据则把第二级的数据给第三级
                        DistrictBean districtBean = new DistrictBean();
                        districtBean.setPareaid(cityBean.getPareaid());
                        districtBean.setAreaname(cityBean.getAreaname());
                        districtBean.setAreaid(cityBean.getAreaid());
                        districtBeanList.add(districtBean);
                        cityBean.setDistrictBeans(districtBeanList);
                    }
                }
            }
        }
        return mProvinceBeanList;
    }

    /**
     * 解析两级数据
     *
     * @return
     */
    public static List<ProvinceBean> parseTwoLevelsData(String response) {
        List<ProvinceBean> mProvinceBeanList = new ArrayList<>();
        NewAddAddressBean areasBean = new Gson().fromJson(response, NewAddAddressBean.class);
        List<NewAddAddressBean.DataBean> dataBeans = areasBean.getData();

        for (NewAddAddressBean.DataBean dataBean1 : dataBeans) {
            //添加省
            if (dataBean1.getPareaid() == 1) {
                ProvinceBean provinceBean = new ProvinceBean();
                provinceBean.setPareaid(dataBean1.getPareaid());
                provinceBean.setAreaname(dataBean1.getAreaname());
                provinceBean.setAreaid(dataBean1.getAreaid());

                for (NewAddAddressBean.DataBean dataBean2 : dataBeans) {
                    //添加市
                    if (dataBean1.getAreaid() == dataBean2.getPareaid()) {
                        CityBean cityBean = new CityBean();
                        cityBean.setPareaid(dataBean2.getPareaid());
                        cityBean.setAreaname(dataBean2.getAreaname());
                        cityBean.setAreaid(dataBean2.getAreaid());

                        provinceBean.getCityBeans().add(cityBean);
                    }

                }
                mProvinceBeanList.add(provinceBean);
            }
        }

        for (ProvinceBean provinceBean : mProvinceBeanList) {
            List<CityBean> cityBeanList = provinceBean.getCityBeans();
            //二级没有   补上
            if (cityBeanList.size() == 0) {
                CityBean cityBean = new CityBean();
                cityBean.setPareaid(provinceBean.getPareaid());
                cityBean.setAreaname(provinceBean.getAreaname());
                cityBean.setAreaid(provinceBean.getAreaid());
                cityBeanList.add(cityBean);
            }
        }
        return mProvinceBeanList;
    }
}
